<?php
/** ============================================================
 * Class for performing filter.
 * @package core
 * ============================================================ */

class Filter
{
    /** Filter name
     * @var string
     */
    var $_name;

    /** Fields types
     * @var array
     */
    var $_fields;

    /** Fields types
     * @var array
     */
    var $_data=array();

    /** Creates new Filter object
     */
    function Filter($name, $fields)
    {
        $this->_name = $name;
        $this->_fields = $fields;
        foreach ($this->_fields as $field=>$type)
        {
            if ('date_range' == $type)
            {
                $this->_data[$field.'_from'] = '';
                $this->_data[$field.'_to'] = '';
            }
            $this->_data[$field] = '';
        }
    }

    /**
     * Enter description here...
     *
     * @param Request $req
     */
    function process($req)
    {
        if ($req->get('filter_'.$this->_name) != 'on')
            return;
        if ($req->get('filter_set')) // set filter
        {
            $url = new Url();
            foreach ($this->_fields as $field=>$type)
            {
                if ('date_range' == $type)
                {
                    $this->_data[$field.'_from'] = $req->get('flt_'.$field.'_from');
                    $this->_data[$field.'_to'] = $req->get('flt_'.$field.'_to');
                    
                    $url->setParam('flt_'.$field.'_from');
                    $url->setParam('flt_'.$field.'_to');
                } elseif('in' == $type)
                {
                    $this->_data[$field] = implode(',',$req->getArray('flt_'.$field));
                    $url->setParam('flt_'.$field);                    
                }
                else
                {
                    $this->_data[$field] = $req->get('flt_'.$field);
                    $url->setParam('flt_'.$field);
                }
            }
            $_SESSION['filters'][$this->_name] = $this->_data;

            $url->setParam('filter_set');
            $url->setParam('filter_'.$this->_name, 'on');
            if ($req->get("alias") == "results")
                    $url->setHost($_SERVER['HTTP_HOST']);
            $req->forward($url->getUrl());
        }
        elseif ($req->get('filter_reset')) //clear filter
        {
            $this->_data = array();
            $_SESSION['filters'][$this->_name] = array();
            unset($_SESSION['filters'][$this->_name]);
            $url = new Url();
            $url->setParam('filter_'.$this->_name);
            $req->forward($url->getUrl());
        }
        elseif (!empty($_SESSION['filters'][$this->_name]))
        {
            $this->_data = $_SESSION['filters'][$this->_name];
        }
    }

    function getCriteria()
    {
        $cond = array();
        if (!$this->_data)
            return $cond;
        $db = Db::get(_cfg('db'));
        foreach ($this->_fields as $field=>$type)
        {
            if ('date_range' == $type)
            {
                $from =  $this->_data[$field.'_from'];
                if ($from)
                    $fromDate = $db->date(strtotime($from.' 00:00:00'));

                $to = $this->_data[$field.'_to'];
                if($to)
                    $toDate   = $db->date(strtotime($to.' 23:59:59'));

                if ($from && $to)
                    $cond[$field] = 'BETWEEN "'.$fromDate.'" AND "'.$toDate.'"';
                elseif ($from)
                    $cond[$field] = '>= "'.$fromDate.'"';
                elseif ($to)
                    $cond[$field] = '<= "'.$toDate.'"';
            }
            elseif ('like' == $type && $this->_data[$field] !== '')
            {
                $v = $db->escape($this->_data[$field]);
                $v = str_replace(array('%', '_'),array('\%', '\_'), $v);
                $cond[$field] = 'LIKE "%'.$v.'%"';
            }
            elseif ('like_e' == $type && $this->_data[$field] !== '')
            {
                $v = $db->escape($this->_data[$field]);
                $v = str_replace(array('%', '_'),array('\%', '\_'), $v);
                $cond[$field] = 'LIKE "% '.$v.' %"';
            }
            elseif ('in' == $type && $this->_data[$field] !== '')
            {
                $cond[$field] = 'IN ('.$this->_data[$field].')';
            }
            elseif (isset($this->_data[$field]) && $this->_data[$field] !== '')
                $cond[$field] = $type.' "'.$db->escape($this->_data[$field]).'"';
    
        }
        return $cond;
    }

    function getInfo()
    {
        $info = array('__name__' => $this->_name); 
        foreach ($this->_fields as $field=>$type)
        {
            if ('date_range' == $type)
            {
                $from = $this->_data[$field.'_from'];
                $fromDate = $from ? _dformat($from, 'Y-m-d') : '';

                $to = $this->_data[$field.'_to'];
                $toDate   = $to ? _dformat($to, 'Y-m-d') : '';

                $info[$field.'_from'] = $fromDate;
                $info[$field.'_to']   = $toDate;
            }
            else
                $info[$field] = $this->_data[$field];
        }
        return $info;
    }
}
?>
